package com.agile.springboot.controller;

import com.agile.springboot.common.Result;
import com.agile.springboot.entity.Question;
import com.agile.springboot.entity.Questionnaire;
import com.agile.springboot.entity.QuestionAnswered;
import com.agile.springboot.entity.User;
import com.agile.springboot.service.QuestionAnsweredService;
import com.agile.springboot.service.QuestionService;
import com.agile.springboot.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/questionnaireAnswered")
public class QuestionAnsweredController {

    @Resource
    UserService userService;
    @Resource
    QuestionAnsweredService questionAnsweredService;

    QuestionService questionService;

    /**
     * 查询当前答者拥有的所有答卷——根据当前答者id
     * @param user 答者实体
     * 所必须的参数为 id 答者的id
     * @return 结果相关信息和 Map<String,List<QuestionAnswered>> 答者拥有的所有答卷,key值为答卷id, value值为该答卷id下的所有已答题目
     */
    @PostMapping("/queryQuestionnaireAnsweredBelongToAnswerer")
    public Result<?> queryQuestionnaireAnsweredBelongToAnswerer(@RequestBody User user){
        User the_user = userService.searchUserById(user.getId());
        if(the_user==null){
            return Result.error("0","不存在此用户id!");
        }
        if(the_user.getIdentity()==3){//辨别用户身份是否为答者
            //查询当前答者拥有的答卷
            Map<String,List<QuestionAnswered>> questionnaireAnsweredList = questionAnsweredService.queryQuestionnaireAnsweredBelongToAnswerer(the_user.getId());
            if(questionnaireAnsweredList.size()!=0){
                return Result.success(questionnaireAnsweredList,"查询成功！");
            }else{
                return Result.error("-2","该答者没有回答过任何问卷！");
            }
        }else {
            return Result.error("-3","此id所对应的不是答者！");
        }
    }

    /**
     * 查询当前问卷所含的所有答卷——根据当前问卷id
     * @param questionnaire 问卷实体
     * 所必须的参数为 id 问卷的id
     * @return 结果相关信息和 Map<String,List<QuestionAnswered>> 问卷所含的所有答卷,key值为答卷id, value值为该答卷id下的所有已答题目
     */
    @PostMapping("/queryQuestionnaireAnsweredBelongToQuestionnaire")
    public Result<?> queryQuestionnaireAnsweredBelongToQuestionnaire(@RequestBody Questionnaire questionnaire){
        //查询当前问卷拥有的答卷
        Map<String,List<QuestionAnswered>> questionAnsweredList = questionAnsweredService.queryQuestionnaireAnsweredBelongToQuestionnaire(questionnaire.getId());
        if(questionAnsweredList.size()!=0){
            return Result.success(questionAnsweredList,"查询成功！");
        }else{
            return Result.error("-1","当前问卷还没有人回答过！");
        }
    }

    /**
     * 查询答卷单卷——根据单卷中的一个答题
     * @param question_answered 答卷中的任意一题
     * 所必须的参数为 questionnaire_answered_id 答卷的id
     * @return 结果相关信息和 Map<String,List<QuestionAnswered>> 该题对应的答卷以及所有题目,key值为答卷id, value值为该答卷id下的所有已答题目
     */
    @PostMapping("/queryQuestionnaireAnsweredById")
    public Result<?> queryQuestionnaireAnsweredById(@RequestBody QuestionAnswered question_answered){
        //查询当前问题所对应的答卷
        Map<String,List<QuestionAnswered>> questionAnsweredList = questionAnsweredService.queryQuestionnaireAnsweredById(question_answered.getQuestionnaire_answered_id());
        if(questionAnsweredList.size()!=0){
            return Result.success(questionAnsweredList,"查询成功！");
        }else{
            return Result.error("-1","该答卷不存在！");
        }
    }

    /**
     * 根据题目类型查询相关的答题情况
     * @param question
     * 所必须的参数为 domain_type 该问题所相关的领域
     * @return 结果相关信息和 List<QuestionAnswered> 该类型问题下的所有答题
     */
    @PostMapping("/queryAnswersByQuestionDomainType")
    public Result<?> queryAnswersByQuestionDomainType(@RequestBody Question question){
        List<QuestionAnswered> questionAnsweredList = questionAnsweredService.queryAnswersByQuestionDomainType(question.getDomain_type());
        if(questionAnsweredList.size()!=0){
            return Result.success(questionAnsweredList,"查询成功！");
        }else{
            return Result.error("-1","无相关题目类型的答题记录！");
        }
    }

    /**
     * 查询单个题目的所有答题——根据一个答题
     * @param question 任意一问题
     * 所必须的参数为 id 问题的id
     * @return 结果相关信息和 List<QuestionAnswered> 该题对应的所有答题
     */
    @PostMapping("/queryQuestionAnsweredByQuestion")
    public Result<?> queryQuestionAnsweredByQuestion(@RequestBody Question question){
        //查询问题所对应的所有答题
        String rootQuestionId = question.getCopy_from();
        if(rootQuestionId.isEmpty()){
            rootQuestionId = question.getId();
        }
        List<Question> sameQuestionList = questionService.searchAllSameQuestionByRootId(rootQuestionId);
        List<QuestionAnswered> allQuestionAnsweredList = new ArrayList<QuestionAnswered>();
        for(Question question1 : sameQuestionList){
            List<QuestionAnswered> questionAnsweredList = questionAnsweredService.queryQuestionAnsweredByQuestionId(question1.getId());
            allQuestionAnsweredList.addAll(questionAnsweredList);
        }

        if(allQuestionAnsweredList.size()!=0){
            return Result.success(allQuestionAnsweredList,"查询成功！");
        }else{
            return Result.error("-1","该题不存在答题记录！");
        }
    }
}
